package com.weixiao.api;

import com.weixiao.model.user.UserInfo;
import com.weixiao.result.Result;
import com.weixiao.service.UserInfoService;
import com.weixiao.utils.AuthContextHolder;
import com.weixiao.vo.user.LoginVo;
import com.weixiao.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * author 韩诚
 * date 2022/10/27
 */
@Api(tags = "登录用户管理")
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 会员登录
     * @param loginVo
     * @return
     */
    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) {
        Map<String, Object> info = userInfoService.login(loginVo);
        return Result.ok(info);
    }

    /**
     * 前台用户认证
     *
     * @param userAuthVo
     * @param request
     * @return
     */
    @ApiOperation(value = "用户认证")
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        //传递两个参数，第一个参数用户id，第二个参数认证数据vo对象
        userInfoService.userAuth(AuthContextHolder.getUserId(request), userAuthVo);
        return Result.ok();
    }

    /**
     * 根据用户id获取用户信息接口
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "根据用户id获取用户信息接口")
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }

    /**
     * 用户详情
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "用户详情")
    @GetMapping("show/{userId}")
    public Result show(@PathVariable Long userId) {
        System.out.println(userId);
        Map<String, Object> map = userInfoService.show(userId);
        return Result.ok(map);
    }

    /**
     * 后台认证审批
     *
     * @param userId
     * @param authStatus
     * @return
     */
    @ApiOperation(value = "认证审批")
    @GetMapping("approval/{userId}/{authStatus}")
    public Result approval(@PathVariable Long userId, @PathVariable Integer authStatus) {
        System.out.println(userId + "    " + authStatus);
        userInfoService.approval(userId, authStatus);
        return Result.ok();
    }
}